package test02;

public class Site implements Runnable {
	private int tickets=100;
	private int nums=0;
	@Override
	public void run() {
		while (true) {
			if (!sale()) {
				break;
			}
		}
	}
	private synchronized  boolean sale() {
		if (tickets<=0) {
			return false;
		}
		tickets--;
		nums++;
		System.out.println(Thread.currentThread().getName()+"抢到第"+nums+"张票，还剩"+tickets+"张票");
		if ("黄牛党".equals(Thread.currentThread().getName())) {
			try {
				wait();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}
	public static void main(String[] args) {
		Runnable site=new Site();
		Thread t1=new Thread(site);
		t1.setName("桃跑跑");
		Thread t2=new Thread(site);
		t2.setName("张票票");
		Thread t3=new Thread(site);
		t3.setName("黄牛党");
		t1.start();
		t2.start();
		t3.start();
		
	}

}
